package com.meadin.agent.sougou;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import com.meadin.agent.domain.BaseMonitor;
import com.meadin.agent.utils.FileUtil;
import com.meadin.agent.utils.StringUtils;

public class SouGouCrawler {

	// 读取品牌的数据
	public List<BaseMonitor> readLine() {
		try {
			String encoding = "UTF-8";
			File file = new File("D:/sou/read/3.txt");
			List<String> list = new ArrayList();
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					list.add(lineTxt);
				}
				read.close();
				return StringUtils.stringToBaseMonitor(list);
			} else {
				System.out.println("找不到指定的文件");
			}

		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return null;
	}

	// 获取url
	public void getUrl() {
		Thread current = Thread.currentThread();
		String name = current.getName();
		List<BaseMonitor> line = readLine();
		for (BaseMonitor base : line) {
			String url = "http://weixin.sogou.com/weixin?type=2&query=" + base.getName()
					+ "&ie=utf8&s_from=input&_sug_=n&_sug_type_=1&w=01015002&oq=&ri=1&sourceid=sugg&sut=0&sst0=1516155869084&lkt=0%2C0%2C0&p=40040108";
			try {
				getDriver(url, base);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	public void visit(WebDriver driver, BaseMonitor base) throws InterruptedException {

		WebElement time = driver.findElement(By.linkText("搜索工具"));
		time.click();
		Thread.sleep(1000L);
		WebElement time2 = driver.findElement(By.linkText("全部时间"));
		time2.click();
		Thread.sleep(1000L);
		WebElement time1 = driver.findElement(By.linkText("一月内"));
		time1.click();
		String html = driver.getPageSource();
		getResult(html, base);

	}

	private void getResult(String result, BaseMonitor base) {

		Document doc = Jsoup.parse(result);
		String select = doc.select("div[class=mun]").text().trim();
		base.setIndexData(StringUtils.getCount(select));
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append(base.getId() + "," + base.getIndexData() + ",1" + "\r\n");
		FileUtil.WriteStringToFile("D:/sou/read/3.txt", sBuffer.toString());
	}

	// 发送请求 获取driver
	public void getDriver(String url, BaseMonitor base) throws Exception {
		try {
			System.setProperty("webdriver.chrome.driver", "C:/dfws/driver/chromedriver.exe");
			ChromeOptions options = new ChromeOptions();
			// 设置浏览器窗口大小
			options.addArguments("--window-size=800,500");
			// 设置无头
			options.addArguments("--headless");
			options.addArguments("--ignore-certificate-errors");
			WebDriver driver = new ChromeDriver(options);
			driver.get(url);
			visit(driver, base);
			driver.close();
			driver.quit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
